﻿using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;

namespace WeChat.CMS;

public class Program
{
    public async static Task<int> Main(string[] args)
    {
        var configuration = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true)
        .AddJsonFile("appsettings.secrets.json", optional: true)
        .Build();

        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
#if DEBUG
                            .MinimumLevel.Debug()
#else
                                .MinimumLevel.Information()
#endif
                            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                            .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
                            .Enrich.FromLogContext()
            .CreateLogger();

        try
        {
            Log.Information("Starting WeChat.CMS.HttpApi.Host.");
            var builder = WebApplication.CreateBuilder(args);
        
             
            builder.Host.AddAppSettingsSecretsJson()
                .UseAutofac()
                .UseSerilog();

         

            await builder.AddApplicationAsync<CMSHttpApiHostModule>();
            var app = builder.Build();
        
            await app.InitializeApplicationAsync();
            //设置url
            //await app.RunAsync("http://*:5001");
            await app.RunAsync();
            return 0;
        }
        catch (Exception ex)
        {
            if (ex is HostAbortedException)
            {
                throw;
            }

            Log.Fatal(ex, "Host terminated unexpectedly!");
            return 1;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }
}
